<!--
 Nested Set Admin GUI
 Form  View File  _form.php

This Form uses client validation.Check Yii Class Reference for rules supported by  client validation.
If your validation rule is not supported you may need to modify this file,possibly enabling
ajax validation -in this case you'll have to write the validation code in the controller.

 @author Spiros Kabasakalis <kabasakalis@gmail.com>,myspace.com/spiroskabasakalis
 @copyright Copyright &copy; 2011 Spiros Kabasakalis
 @since 1.0
 @license The MIT License
-->
<style>
    div#imgUpload{ display: none}
    #file{
        margin-left: 0px;
    }
</style>

<div id="menu_form_con"   class="client-val-form">
    <?php if(Yii::app()->request->getPost('create_root')&&$this->model->isNewRecord):?>              <h3 id="create_header"><?php echo Yii::t('main', 'Create New Root Category') ?> </h3>
    <?php elseif($this->model->isNewRecord):?>     <h3 id="create_header"><?php echo Yii::t('main', 'Create New Category') ?> </h3>
    <?php elseif(!$this->model->isNewRecord):?>     <h3 id="update_header"><?php echo $this->model->name;?></h3>
    <?php endif;?>
    <div id="success-menu" class="notification success png_bg" style="display:none;">
        <a href="#" class="close"><img src="<?php echo Yii::app()->getBaseUrl().'/css/images/icons/cross_grey_small.png';?>" title="Close this notification" alt="close" /></a>
    </div>
    <div id="error-menu" class="notification errorshow png_bg" style="display:none;">
        <a href="#" class="close"><img src="<?php echo Yii::app()->getBaseUrl().'/css/images/icons/cross_grey_small.png';?>" title="Close this notification" alt="close" /></a>
    </div>
    <div class="form">
        <?php
        $formId='menu-form';
$create_root=Yii::app()->request->getPost('create_root');
$ajaxUrl=($this->model->isNewRecord)?(($create_root!='true')?CController::createUrl('productCategory/create'):CController::createUrl('productCategory/createRoot')):CController::createUrl('productCategory/update');
$val_error_msg='Error.ProductCategory was not saved.';
$val_success_message=($this->model->isNewRecord)?(($create_root!='true')?'ProductCategory was created successfuly.':'Root ProductCategory was created successfuly.'):'ProductCategory was updated successfuly.';
$success='function (data) {
    var response = jQuery.parseJSON(data);
    if (response.success == true) {
        jQuery("#'.ProductCategory::ADMIN_TREE_CONTAINER_ID.'").jstree("refresh");
        $("#success-menu").fadeOut(1000, "linear", function () {
            $(this).append("<div> '.$val_success_message.'</div>").fadeIn(2000, "linear")
        });
        $("#menu-form").slideToggle(1500);
    }
    else {
        $("#error-menu").hide().show().css({
            "opacity": 1
        }).append("<div>" + response.message + "</div>").fadeIn(2000);
        jQuery("#'.ProductCategory::ADMIN_TREE_CONTAINER_ID.'").jstree("refresh");
    }
} //function';
$js_afterValidate="js: function (form, data, hasError) {
    if (!hasError) { //if there is no error submit with  ajax
        jQuery.ajax({
            'type': 'POST',
            'url': '$ajaxUrl',
            'cache': false,
            'data': $(\"#$formId\").serialize(),
            'success':$success
        });
        return false; //cancel submission with regular post request,ajax submission performed above.
    } //if has not error submit via ajax

    else{
        return false;       //if there is validation error don't send anything
    }                    //cancel submission with regular post request,validation has errors.
}";
$form=$this->beginWidget('CActiveForm',array(
    'id'=>'menu-form',// 'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'focus'=>array($this->model,'name'),
    'errorMessageCssClass'=>'input-notification-error  error-simple png_bg',
    'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'validateOnType'=>false,
            'afterValidate'=>$js_afterValidate,
            'errorCssClass'=>'err',
            'successCssClass'=>'suc',
            'afterValidateAttribute'=>'js: function (form, attribute, data, hasError) {
                    if (!hasError) {
                        $("#success-" + attribute.id).fadeIn(500);
                        $("label[for=\'ProductCategory_" + attribute.name + "\']").removeClass("error");
                    }
                    else {
                        $("label[for=\'ProductCategory_" + attribute.name + "\']").addClass("error");
                        $("#success-" + attribute.id).fadeOut(500);
                    }
            }'
    ),
));
?>
        <div id="imgUpload"></div>
        <?php echo $form->errorSummary($this->model, '<div style="font-weight:bold">Please correct these errors:</div>', NULL, array('class' => 'errorsum notification errorshow png_bg')); ?>
        <div class="row" >
            <?php echo $form->labelEx($this->model, 'name'); ?>
            <?php echo $form->textField($this->model, 'name', array('size' => 60, 'maxlength' => 128, 'value' => Yii::app()->request->getPost('name'), 'style' => 'width:88%;')); ?>       <span  id="success-ProductCategory_name"  class="hid input-notification-success  success png_bg"></span>
            <div><small></small> </div>
            <?php echo $form->error($this->model, 'name'); ?>
        </div>
        
        <div class="row">
            <?php echo CHtml::label(Yii::t('main', 'Upload'), ''); ?>
            <div id="file">
                <div id="thumb">
                    <?php if($this->model->avatar): ?>
                        <img src="<?php echo Yii::app()->baseUrl . '/' . $this->model->avatar->url ?>" width="100" height="100" />
                    <?php endif ?>
                </div>
                
                <div id="container">
                    <div id="filelist"></div>
                    <a id="pickfiles" href="#">[Select files]</a>
                    <a id="uploadfiles" href="#">[Upload files]</a>
                </div>
            </div>
        </div>
        
        <div class="row">
            <?php echo $form->labelEx($this->model, 'description'); ?>
            <?php echo $form->textArea($this->model, 'description', array('rows' => 6, 'cols' => 50)); ?>
            <span  id="success-ProductCategory_description"  class="hid input-notification-success  success png_bg"></span>
            <div><small></small> </div>
            <?php echo $form->error($this->model, 'description'); ?>
        </div>
        
        <input type="hidden" name= "YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"  />
        <input type="hidden" name= "parent_id" value="<?php echo Yii::app()->request->getPost('parent_id'); ?>"  />
        <?php if (!$this->model->isNewRecord): ?>
        <input type="hidden" name= "update_id" value="<?php echo Yii::app()->request->getPost('update_id'); ?>"/>
        <?php endif; ?>      
        <div class="row buttons">
            <?php echo CHtml::submitButton($this->model->isNewRecord?'Submit':'Save',array('class'=>'button align-right'));?>	</div>
        <?php $this->endWidget();?></div><!-- form -->
</div>
<script  type="text/javascript">
    $(function(){
        var uploader = new plupload.Uploader({
            runtimes : 'gears,html5,flash,silverlight,browserplus',
            browse_button : 'pickfiles',
            container : 'container',
            max_file_size : '50mb',
            url : '<?php echo $this->createUrl('upload') ?>',
            flash_swf_url : '<?php echo Yii::app()->baseUrl ?>/js/plupload/plupload.flash.swf',
            silverlight_xap_url : '<?php echo Yii::app()->baseUrl ?>/js/plupload/plupload.silverlight.xap',
            filters : [
                {title : "Image files", extensions : "jpg,gif,png"},
                {title : "Zip files", extensions : "zip"}
            ],
            resize : {width : 320, height : 240, quality : 90}
        });
        
        uploader.init();
        
        $('#uploadfiles').click(function(e) {
            //uploader in parent file: admin.php            
            uploader.start();
            e.preventDefault();
        });
        
        uploader.bind('FilesAdded', function(up, files) {
            $.each(files, function(i, file) {
                $('#filelist').html(
                '<div id="' + file.id + '" class="imgItem">' +
                    file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                    '</div>');

            });

            up.refresh(); // Reposition Flash/Silverlight
        });

        uploader.bind('UploadProgress', function(up, file) {  
            $('#' + file.id+" b").html(file.percent + "%");

        });

        uploader.bind('Error', function(up, err) {
            $('#filelist').append("<div>Error: " + err.code +
                ", Message: " + err.message +
                (err.file ? ", File: " + err.file.name : "") +
                "</div>"
        );

            up.refresh(); // Reposition Flash/Silverlight
        });

        uploader.bind('FileUploaded', function(up, file, response) {            
            var resObj=$.parseJSON(response.response);
            $('#' + file.id + " b").html("100%");
            $('#imgUpload').html('<input name="ProductCategory[imgId]" value="'+resObj.id+'">');
            $('#file #thumb').html('<img src="'+resObj.url+'" width="100" height="100" />');                
        });
    });
    
    
    
    //Close button:
    
    $(".close").click(
        function () {
            $(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
                $(this).slideUp(600);
            });
            return false;
        }
    );
</script>